package demo

import (
	"app/internal/errcode"
	"app/internal/helper"
	"app/internal/model"
	protoDemo "app/internal/proto/demo"
	"app/wd"
	"fmt"

	"github.com/gin-gonic/gin"
)

// Demo demo
func (b *Biz) Demo(ctx *gin.Context, req *protoDemo.Req) (interface{}, *wd.ResponseError) {
	db := wd.App.Mysql["default"].DB.Table("tb_t1")
	sqlBuild := helper.SqlBuilder{
		DB:         db,
		Field:      "*",
		Where:      "",
		WhereParam: []interface{}{},
		IsPage:     true,
		Limit:      req.PageSize,
		Page:       req.Page,
	}
	res := protoDemo.Resp{
		Page:     sqlBuild.GetPage(),
		PageSize: sqlBuild.GetLimit(),
	}

	list := make([]*model.TbDemo, 0)
	err := sqlBuild.GetList(ctx, &list)
	if err != nil {
		return nil, wd.NewResponseError(errcode.DemoErr.ToErr(err))
	}
	res.List = list
	res.Count, err = sqlBuild.GetCount(ctx)
	if err != nil {
		return nil, wd.NewResponseError(errcode.DemoErr.ToErr(err))
	}
	return &res, nil
}

// AddDemo 添加demo
func (b *Biz) AddDemo() *model.TbDemo {
	db := wd.App.Mysql["default"].DB.Table("tb_t1")
	xx := &model.TbDemo{
		Name: "test",
		Age:  10,
	}
	err := db.Create(xx).Error
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(xx.ID)
	return xx
}
